home *** CD-ROM | disk | FTP | other *** search
/ Freelog 22 / freelog 22.iso / Prog / Djgpp / GPC2952B.ZIP / doc / gpc / demos / gpc_c_pas.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-02-08  |  2.3 KB  |  78 lines

  1. {
  2. GPC demo program to show how to include Pascal code in C programs.
  3.  
  4. Copyright (C) 2000-2001 Free Software Foundation, Inc.
  5.  
  6. Author: Frank Heckenbach <frank@pascal.gnu.de>
  7.  
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, version 2.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; see the file COPYING. If not, write to
  19. the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. Boston, MA 02111-1307, USA.
  21.  
  22. As a special exception, if you incorporate even large parts of the
  23. code of this demo program into another program with substantially
  24. different functionality, this does not cause the other program to
  25. be covered by the GNU General Public License. This exception does
  26. not however invalidate any other reasons why it might be covered
  27. by the GNU General Public License.
  28. }
  29.  
  30. program GPC_C_Pas;
  31.  
  32. uses GPC_C_Unit;
  33.  
  34. { Tell the compiler not to create `main', but something else. }
  35. {$gpc-main=Dummy}
  36.  
  37. { We link the C code using a compiler directive. Although the C code
  38.   contains the main program, the linker doesn't mind if we do it
  39.   like this and compile the Pascal program. But you can just as well
  40.   compile the Pascal code with `-c' and link it to the C code
  41.   explicitly. }
  42. {$L gpc_c_c.c}
  43.  
  44. { External declarations we use from the C code }
  45.  
  46. var
  47.   CVariable : Integer; asmname 'c_variable'; external;
  48.  
  49. procedure CRoutine; asmname 'c_routine';
  50.  
  51. { Pascal code }
  52.  
  53. var
  54.   PascalProgramVariable : Integer = 17; asmname 'pascal_program_variable';
  55.  
  56. procedure PascalProgramRoutine; asmname 'pascal_program_routine';
  57. procedure PascalProgramRoutine;
  58. begin
  59.   Writeln ('Routine in Pascal program called from C code.');
  60.   Writeln ('PascalProgramVariable is now ', PascalProgramVariable, '.');
  61.  
  62.   Writeln ('Decrementing CVariable.');
  63.   Dec (CVariable);
  64.  
  65.   Writeln ('Calling CRoutine.');
  66.   Flush (Output);
  67.   CRoutine;
  68.  
  69.   Writeln ('Back in Pascal program''s routine');
  70.   Flush (Output)
  71. end;
  72.  
  73. { This is not the main program in this setting! }
  74. begin
  75.   Writeln ('This is never called.');
  76.   Flush (Output)
  77. end.
  78.